package sw.EngAdroid;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.util.DisplayMetrics;
import android.view.Window;

public class SixPlayerActivity extends Activity{

	private TextView currentName;
	private TextView questionText;
	private TextView question;
	private EditText answerText;
	private ArrayList<String> playerNames;
	private Button submit;
	private boolean notdone;
	private int currentPlayer;
	private Game game;
	private int numberOfPlayers;
	private TextView myCounter;
	private sw.EngAdroid.CountDownTimer t;
	
	private Button bet0;
	private TextView guess0;
	private TextView ratio0;
	private EditText chips0;
	
	private Button bet1;
	private TextView guess1;
	private TextView ratio1;
	private EditText chips1;
	
	private Button bet2;
	private TextView guess2;
	private TextView ratio2;
	private EditText chips2;
	
	private Button bet3;
	private TextView guess3;
	private TextView ratio3;
	private EditText chips3;
	
	private Button bet4;
	private TextView guess4;
	private TextView ratio4;
	private EditText chips4;
	
	private Button bet5;
	private TextView guess5;
	private TextView ratio5;
	private EditText chips5;
	
	private TextView p0chips;
	private TextView p1chips;
	private TextView p2chips;
	private TextView p3chips;
	private TextView p4chips;
	private TextView p5chips;
	
	private TextView correctGuess;
	private Button nextRound;
	private Button endGame;
	private TextView playerNameBet;
	private Boolean isLocal = true;
	private boolean small = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		if(dm.widthPixels<1000){
			setContentView(R.layout.question_screen2);
			small = true;
		}
		else
			setContentView(R.layout.question_screen);
		startGame();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	private void startGame(){
		currentPlayer = 0;
		notdone = true;
		Intent intentInfo = getIntent();
		Bundle info = intentInfo.getExtras();
		playerNames = info.getStringArrayList("playerNames");
		numberOfPlayers = playerNames.size();
		if(isLocal == true)
			game = new Game("Test", playerNames.size());
		else
			game = new NetworkGame("Test", playerNames.size());
		for(int i = 0; i<playerNames.size(); i++){
			Player p = new Player(playerNames.get(i));
			p.joinGame(game);
		}
			placeGuess();
	}
	
	private void placeGuess(){
		if (small)
			setContentView(R.layout.question_screen2);
		else
			setContentView(R.layout.question_screen);
		
		currentName = (TextView)findViewById(R.id.playerName);
		questionText = (TextView)findViewById(R.id.questionText);
		answerText = (EditText)findViewById(R.id.answerTextBox);
		submit = (Button)findViewById(R.id.submitAnswerButton);
		currentName.setText(((Player)(game.getPlayers().get(currentPlayer))).getPlayer_name());
		questionText.setText(game.getQuestion().getText());
		myCounter = (TextView)findViewById(R.id.timer);
		
		t = new sw.EngAdroid.CountDownTimer(30000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
		    	 myCounter.setText("Time Remaining: " + millisUntilFinished / 1000);
		    	 
		     }

		     @Override
			public void onFinish() {
		    	 myCounter.setText("Times up!");
		    	 answerText.setText("000000");
		    	 getAnswer();
		    	 submit.performClick();
		     }
		  }.start();
		  
		  getAnswer();

	}
	
	private void placeBets(){
		if (small)
			setContentView(R.layout.playerboard6small);
		else
			setContentView(R.layout.playerboard6);
		
		setContentView(R.layout.playerboard6);
		bet0 = (Button)findViewById(R.id.confirm01);
		question = (TextView)findViewById(R.id.questionTextView1);
		guess0 = (TextView)findViewById(R.id.guess01);
		ratio0 = (TextView)findViewById(R.id.ratio01);
		chips0 = (EditText)findViewById(R.id.chips01);
		//bet1 = (Button)findViewById(R.id.confirm02);
		
		guess1 = (TextView)findViewById(R.id.guess02);
		ratio1 = (TextView)findViewById(R.id.ratio02);
		chips1 = (EditText)findViewById(R.id.chips02);
		//bet2 = (Button)findViewById(R.id.confirm03);
		
		guess2 = (TextView)findViewById(R.id.guess03);
		ratio2 = (TextView)findViewById(R.id.ratio03);
		chips2 = (EditText)findViewById(R.id.chips03);
		
		guess3 = (TextView)findViewById(R.id.guess04);
		ratio3 = (TextView)findViewById(R.id.ratio04);
		chips3 = (EditText)findViewById(R.id.chips04);
		
		guess4 = (TextView)findViewById(R.id.guess05);
		ratio4 = (TextView)findViewById(R.id.ratio05);
		chips4 = (EditText)findViewById(R.id.chips05);
		
		guess5 = (TextView)findViewById(R.id.guess06);
		ratio5 = (TextView)findViewById(R.id.ratio06);
		chips5 = (EditText)findViewById(R.id.chips06);
		
		playerNameBet = (TextView)findViewById(R.id.gameNameView);
		playerNameBet.setText("Player name: " + ((Player)game.getPlayers().get(currentPlayer)).getPlayer_name());
		
		List<Guess> guesses = game.getGuesses();
		question.setText(game.getQuestion().getText());
		ratio0.setText("Ratio: 2.0");
		ratio1.setText("Ratio: 2.0");
		ratio2.setText("Ratio: 2.0");
		ratio3.setText("Ratio: 2.0");
		ratio4.setText("Ratio: 2.0");
		ratio5.setText("Ratio: 2.0");
		
		guess0.setText("Guess: " + guesses.get(0).getGuess());
		guess1.setText("Guess: " + guesses.get(1).getGuess());
		guess2.setText("Guess: " + guesses.get(2).getGuess());
		guess3.setText("Guess: " + guesses.get(3).getGuess());
		guess4.setText("Guess: " + guesses.get(4).getGuess());
		guess5.setText("Guess: " + guesses.get(5).getGuess());
		
		myCounter = (TextView)findViewById(R.id.Timer);
		
		t = new sw.EngAdroid.CountDownTimer(30000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
		    	 myCounter.setText("Time Remaining: " + millisUntilFinished / 1000);
		    	 
		     }

		     @Override
			public void onFinish() {
		    	 myCounter.setText("Times up!");
		    	 bet0.performClick();
		     }
		  }.start();
		
		bet0.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v){
				t.cancel();
				Player player = ((Player)game.getPlayers().get(currentPlayer));
				String one = chips0.getText().toString();
				if(!(one.equals("")))
					player.placeBet(0, Integer.parseInt(one));
				String two = chips1.getText().toString();
				if(!(two.equals("")))
					player.placeBet(1, Integer.parseInt(two));
				String three = chips2.getText().toString();
				if(!(three.equals("")))
					player.placeBet(2, Integer.parseInt(three));
				String four = chips3.getText().toString();
				if(!(four.equals("")))
					player.placeBet(3, Integer.parseInt(four));
				String five = chips4.getText().toString();
				if(!(five.equals("")))
					player.placeBet(4, Integer.parseInt(five));
				String six = chips5.getText().toString();
				if(!(six.equals("")))
					player.placeBet(5, Integer.parseInt(six));
				if(currentPlayer<(game.getPlayers().size()-1)){
					currentPlayer++;
					placeBets();
				}
				else{
					currentPlayer=0;
					game.resetChipBets();
					winnings();
				}
			}
		});
//		bet1.setOnClickListener(new Button.OnClickListener() {
//			public void onClick(View v){
//				Player player = ((Player)game.getPlayers().get(currentPlayer));
//				player.placeBet(1, Integer.parseInt(chips1.getText().toString()));
//				if(currentPlayer<(game.getPlayers().size()-1)){
//					currentPlayer++;
//					placeBets();
//				}
//				else{
//					currentPlayer=0;
//					winnings();
//				}
//			}
//		});
//		bet2.setOnClickListener(new Button.OnClickListener() {
//			public void onClick(View v){
//				Player player = ((Player)game.getPlayers().get(currentPlayer));
//				player.placeBet(2, Integer.parseInt(chips2.getText().toString()));
//				if(currentPlayer<(game.players.size()-1)){
//					currentPlayer++;
//					placeBets();
//				}
//				else{
//					currentPlayer=0;
//					winnings();
//				}
//			}
//		});
	}
	
	private void winnings(){
		if (small)
			setContentView(R.layout.winnings6small);
		else
			setContentView(R.layout.winnings6);
		
		game.winnings();
		p0chips = (TextView)findViewById(R.id.player0);
		p1chips = (TextView)findViewById(R.id.player1);
		p2chips = (TextView)findViewById(R.id.player2);
		p3chips = (TextView)findViewById(R.id.player3);
		p4chips = (TextView)findViewById(R.id.player4);
		p5chips = (TextView)findViewById(R.id.player5);
		
		correctGuess = (TextView)findViewById(R.id.correctGuess);
		correctGuess.setText("Correct answer: " + game.getQuestion().getAnswer());
		
		p0chips.setText(game.getPlayers().get(0).getPlayer_name() +"'s chips: " + game.getPlayers().get(0).getChips());
		p1chips.setText(game.getPlayers().get(1).getPlayer_name() +"'s chips: " +game.getPlayers().get(1).getChips());
		p2chips.setText(game.getPlayers().get(2).getPlayer_name() +"'s chips: " +game.getPlayers().get(2).getChips());
		p3chips.setText(game.getPlayers().get(3).getPlayer_name() +"'s chips: " +game.getPlayers().get(3).getChips());
		p4chips.setText(game.getPlayers().get(4).getPlayer_name() +"'s chips: " +game.getPlayers().get(4).getChips());
		p5chips.setText(game.getPlayers().get(5).getPlayer_name() +"'s chips: " +game.getPlayers().get(5).getChips());
		
		
		nextRound = (Button)findViewById(R.id.again);
		endGame = (Button)findViewById(R.id.endGame);
		game.displayAnswer();
		//game.clearBets();
		//game.clearGuesses();
		
		nextRound.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				//int temp = game.nextQuestion;
				//temp++;
				//game.nextQuestion = temp;
				placeGuess();
			}
		});
		
		endGame.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v){
				finish();
			}
		});
		
		
	}

	private void getAnswer(){
		submit.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Player player = ((Player)game.getPlayers().get(currentPlayer));
				int numGuess = Integer.parseInt(answerText.getText().toString());
				//Guess guess = new Guess(player.getPlayer_name(), numGuess);
				player.submitGuess(numGuess);
				if(currentPlayer<(game.getPlayers().size()-1)){
					currentPlayer++;
					t.cancel();
					placeGuess();
				}
				else{
					t.cancel();
					currentPlayer = 0;
					placeBets();				
				}
			}
		});
	}
}
